//
//  MCWechatRechargeVC.m
//  TLYL
//
//  Created by MC on 2018/1/12.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCWechatRechargeVC.h"
#import "MCPayWebViewController.h"
#import "NSString+URL.h"

@interface MCWechatRechargeVC ()

@property (nonatomic,strong)UILabel * czMoneyLbl;

@property (nonatomic,strong)UILabel * orderNumberLbl;

@property (nonatomic,strong)UIButton * sureBtn;

@property (nonatomic,strong)UIButton * dismissBtn;

@property (nonatomic,strong)UITextField * payCodeTf;


@end

@implementation MCWechatRechargeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setProperty];
    
    
    [self createUI];
    
    
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.view.backgroundColor=RGB(231, 231, 231);
    self.title=@"智通宝微信反扫";
    
}

#pragma mark==================createUI======================
-(void)createUI{
    
    
    //    self.orderNumberLbl= [self GetAdaptiveLable:CGRectMake(19, 20, G_SCREENWIDTH-38, 40) AndText:[NSString stringWithFormat:@"订单编号：%@",_rechargeModel.OrderID] andFont:12 andBackColor:[UIColor clearColor] andTextColor:RGB(46,46,46) andTextAlignment:NSTextAlignmentLeft];
    //    [self.view addSubview:self.orderNumberLbl];
    //    self.orderNumberLbl.font=[UIFont systemFontOfSize:12];
    //    self.orderNumberLbl.numberOfLines=0;
    //
    //
    //    self.czMoneyLbl= [self GetAdaptiveLable:CGRectMake(19, 60, G_SCREENWIDTH-38, 40) AndText:[NSString stringWithFormat:@"充值金额：%@",_rechargeModel.PayMoney] andFont:12 andBackColor:[UIColor clearColor] andTextColor:RGB(46,46,46) andTextAlignment:NSTextAlignmentLeft];
    //    [self.view addSubview:self.czMoneyLbl];
    
    
    
    
    _payCodeTf = [self GetTfWithkeyboardType:UIKeyboardTypeNumberPad];
    _payCodeTf.backgroundColor=[UIColor whiteColor];
    _payCodeTf.placeholder=@"请输入付款码数字";
    [self.view addSubview:_payCodeTf];
    [_payCodeTf mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left).offset(19);
        make.right.equalTo(self.view.mas_right).offset(-19);
        //        make.width.mas_equalTo(150);
        make.height.mas_equalTo(40);
        make.top.equalTo(self.view.mas_top).offset(10);
        
    }];
    
    
    UIButton * sureBtn = [[UIButton alloc]init];
    [self.view addSubview:sureBtn];
    sureBtn.backgroundColor=RGB(255,168,0);
    [sureBtn setTitle:@"前往支付" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    sureBtn.layer.cornerRadius=6;
    sureBtn.clipsToBounds=YES;
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(80);
        make.left.equalTo(self.view.mas_left).offset(19);
        make.width.mas_equalTo((G_SCREENWIDTH-20)/2.0-10);
        make.height.mas_equalTo(40);
    }];
    [sureBtn addTarget:self action:@selector(action_sure) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton * dismissBtn = [[UIButton alloc]init];
    [self.view addSubview:dismissBtn];
    dismissBtn.backgroundColor=MC_THEME_COLOR;
    [dismissBtn setTitle:@"取消" forState:UIControlStateNormal];
    [dismissBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    dismissBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    dismissBtn.layer.cornerRadius=6;
    dismissBtn.clipsToBounds=YES;
    [dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(80);
        make.right.equalTo(self.view.mas_right).offset(-19);
        make.width.mas_equalTo((G_SCREENWIDTH-38-10)/2.0);
        make.height.mas_equalTo(40);
    }];
    [dismissBtn addTarget:self action:@selector(action_dismiss) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UILabel * tipLbl= [self GetAdaptiveLable:CGRectMake(19, 150, G_SCREENWIDTH-38, 14) AndText:@"打开微信钱包->收付款->点击条形码查看付款码数字" andFont:12 andBackColor:[UIColor clearColor]  andTextColor:RGB(249,84,83) andTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:tipLbl];
    
    
}

-(void)action_dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)action_sure{
    
    _payCodeTf.text = [_payCodeTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (_payCodeTf.text.length<1) {
        [SVProgressHUD showInfoWithStatus:@"请输入付款码！"];
    }
    MCPayWebViewController * VC = [[MCPayWebViewController alloc]init];
    NSString * URL=_rechargeModel.BankUrl;
    
    NSArray * MValueArr=[_rechargeModel.MValue componentsSeparatedByString:@"$"];
    
    NSString * strValue =MValueArr[0];
    
    NSString * T_strValue =MValueArr[0];
    
    for (int i=1;i<MValueArr.count;i++) {
        
        if (i%2==0) {
            strValue=[NSString stringWithFormat:@"%@%26%@",strValue,MValueArr[i]];
        }else{
            strValue = [strValue stringByAppendingString:@"%3D"];
            strValue=[NSString stringWithFormat:@"%@%@",strValue,MValueArr[i]];
            
        }
    }
    
    for (int i=1;i<MValueArr.count;i++) {
        
        if (i%2==0) {
            NSString * temp = [MValueArr[i] URLEncodedString];
            T_strValue=[NSString stringWithFormat:@"%@&%@",T_strValue, temp];
            
        }else{
            
            NSString * temp = [MValueArr[i] URLEncodedString];
            T_strValue=[NSString stringWithFormat:@"%@=%@",T_strValue,temp];
            
        }
        
    }
    
    T_strValue = [NSString stringWithFormat:@"%@&auth_code=%@",T_strValue,_payCodeTf.text];
    
    VC.url=[NSString stringWithFormat:@"%@?%@",URL,T_strValue];
    
    
    
    
    [self.navigationController pushViewController:VC animated:YES];
    
}

-(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andFont:(CGFloat)font andBackColor:(UIColor*)backColor andTextColor:(UIColor *)textColor andTextAlignment:(NSTextAlignment)textAlignment;
{
    UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
    contentLbl.text = contentStr;
    contentLbl.textAlignment = textAlignment;
    contentLbl.font = [UIFont systemFontOfSize:font];
    contentLbl.textColor=textColor;
    contentLbl.numberOfLines=0;
    contentLbl.backgroundColor=backColor;
    contentLbl.clipsToBounds=YES;
    
    return contentLbl;
}
-(UITextField *)GetTfWithkeyboardType:(UIKeyboardType)keyboardType{
    
    UITextField * textField=[[UITextField alloc]init];
    textField.layer.cornerRadius=3;
    textField.clipsToBounds=YES;
    textField.layer.borderWidth=0.5f;
    textField.layer.borderColor=RGB(181,181,181).CGColor;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.returnKeyType = UIReturnKeyDone;
    textField.keyboardType = keyboardType;//UIKeyboardTypeDecimalPad
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    textField.font=[UIFont systemFontOfSize:12];
    textField.textColor=RGB(40,40,40);
    [textField setValue:[UIFont systemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    textField.inputAccessoryView = [self addToolbar];
    return textField;
    
}
- (UIToolbar *)addToolbar{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, 35)];
    toolbar.tintColor = RGB(58, 130, 208);
    toolbar.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space, bar];
    return toolbar;
}

//完成
-(void)textFieldDone{
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

